package com.cssw.fyzb.busi.api.video;

import com.cssw.fyzb.busi.service.video.VideoStatService;
import com.cssw.fyzb.core.model.dodb.CmfUserMusic;
import com.cssw.fyzb.core.model.dodb.CmfUserVideo;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @description:
 * @author: zzf
 * @time: 2021/1/19 20:21
 */
@Function(description = "视频审核完加es", parameters = {
        @Parameter(name = "id", type = Type.String, description = "视频标识", required = true),
})
@Component
public class AddVideoEs implements Action {

    @Autowired
    private VideoStatService videoStatService;

    private static final Logger logger = LoggerFactory.getLogger(AddVideoEs.class);


    @Override
    public Response execute(Request request) throws Exception {
        Long id = request.getLong("id");
        try {
            CmfUserVideo cmfUserVideo = videoStatService.getCmfUserVideo(id);
            if (null == cmfUserVideo || cmfUserVideo.getIsdel()){
                return Response.fail("视频已经删除");
            }

            CmfUserMusic cmfUserMusic = null;
            if (cmfUserVideo.getMusicId() != null && cmfUserVideo.getMusicId() != 0) {
                cmfUserMusic = videoStatService.getCmfUserMusic(cmfUserVideo.getMusicId());
            }
            if (cmfUserMusic == null) {
                cmfUserMusic = new CmfUserMusic();
            }

            videoStatService.save(Long.valueOf(cmfUserVideo.getUid()), cmfUserVideo.getId(), cmfUserVideo.getTitle(), cmfUserVideo.getHref(), cmfUserVideo.getThumb(),
                    cmfUserVideo.getCity(), cmfUserVideo.getLat(), cmfUserVideo.getLng(), cmfUserVideo.getAddtime(),
                    cmfUserVideo.getMusicId(), cmfUserMusic.getTitle(), cmfUserMusic.getAuthor(), cmfUserMusic.getImgUrl(), cmfUserMusic.getLength()
                    , cmfUserMusic.getFileUrl(), cmfUserMusic.getTitle(), cmfUserVideo.getAnyway(), cmfUserVideo.getClassid());
            return Response.success();
        } catch (Exception e) {
            logger.info("", e);
            return Response.fail("操作异常");
        }
    }
}

